package org.bonorsoft.modules.system.controller;

import java.util.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.constant.UserConstants;
import org.bonorsoft.common.system.query.QueryGenerator;
import org.bonorsoft.common.util.DateUtils;
import org.bonorsoft.common.util.oConvertUtils;
import org.bonorsoft.modules.system.entity.SysDict;
import org.bonorsoft.modules.system.service.ISysDictService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;

 /**
 * @Description: 字典类别
 * @Author: WilliamYe
 * @Date:   2020-11-18
 * @Version: V1.0
 */
@Slf4j
@Controller
@RequestMapping("/system/dict")
public class SysDictController extends BonorController<SysDict, ISysDictService> {
	@Autowired
	private ISysDictService sysDictService;

	 private String prefix = "system/dict/type";

	 @RequiresPermissions("system:dict:view")
	 @GetMapping()
	 public String dictType()
	 {
		 return prefix + "/type";
	 }

	/**
	 * 分页列表查询
	 *
	 * @param sysDict
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "字典类别-分页列表查询")
	@GetMapping("/list")
	@RequiresPermissions("system:dict:list")
	@ResponseBody
	public Result<?> queryPageList(SysDict sysDict,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SysDict> queryWrapper = QueryGenerator.initQueryWrapper(sysDict, req.getParameterMap());
		Page<SysDict> page = new Page<SysDict>(pageNo, pageSize);
		IPage<SysDict> pageList = sysDictService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	 /**
	  * 新增字典类型
	  */
	 @GetMapping("/add")
	 public String add()
	 {
		 return prefix + "/add";
	 }

	/**
	 * 添加
	 *
	 * @param sysDict
	 * @return
	 */
	@AutoLog(value = "字典类别-添加")
	@RequiresPermissions("system:dict:add")
	@PostMapping(value = "/add")
	@ResponseBody
	public Result<?> add(@Validated SysDict sysDict) {
		if (UserConstants.DICT_TYPE_NOT_UNIQUE.equals(sysDictService.checkDictTypeUnique(sysDict)))
		{
			return Result.error("新增字典'" + sysDict.getDictName() + "'失败，字典类型已存在");
		}
		sysDict.setCreateBy(getLoginUserName());
		sysDict.setCreateTime(DateUtils.getDate());
		sysDictService.save(sysDict);
		return Result.ok("添加成功！");
	}

	 /**
	  * 修改字典类型
	  */
	 @GetMapping("/edit/{id}")
	 public String edit(@PathVariable("id") String id, ModelMap mmap)
	 {
		 mmap.put("dict", sysDictService.getById(id));
		 return prefix + "/edit";
	 }

	/**
	 * 编辑
	 *
	 * @param sysDict
	 * @return
	 */
	@AutoLog(value = "字典类别-编辑")
	@PostMapping(value = "/edit")
	@RequiresPermissions("system:dict:edit")
	@ResponseBody
	public Result<?> edit(@Validated SysDict sysDict) {
		if (UserConstants.DICT_TYPE_NOT_UNIQUE.equals(sysDictService.checkDictTypeUnique(sysDict)))
		{
			return Result.error("修改字典'" + sysDict.getDictName() + "'失败，字典类型已存在");
		}
		sysDict.setUpdateBy(getLoginUserName());
		sysDict.setUpdateTime(DateUtils.getDate());
		sysDictService.updateById(sysDict);
		return Result.ok("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "字典类别-通过id删除")
	@DeleteMapping(value = "/delete")
	@ResponseBody
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		sysDictService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "字典类别-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	@RequiresPermissions("system:dict:remove")
	@ResponseBody
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.sysDictService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "字典类别-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		SysDict sysDict = sysDictService.getById(id);
		return Result.ok(sysDict);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param sysDict
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, SysDict sysDict) {
      return super.exportXls(request, sysDict, SysDict.class, "字典类别");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, SysDict.class);
  }

	 /**
	  * 校验字典类型
	  */
	 @PostMapping("/checkDictTypeUnique")
	 @ResponseBody
	 public String checkDictTypeUnique(SysDict dictType)
	 {
	 	return sysDictService.checkDictTypeUnique(dictType);
	 }

	 /**
	  * 查询字典详细
	  */
	 @GetMapping("/detail/{id}")
	 public String detail(@PathVariable("id") String id, ModelMap mmap)
	 {
		 mmap.put("dict", sysDictService.getById(id));
		 mmap.put("dictList", sysDictService.list());
		 return "system/dict/data/data";
	 }

	 /**
	  * 清空缓存
	  */
	 @RequiresPermissions("system:dict:remove")
	 @AutoLog(value = "字典类别-清空缓存")
	 @GetMapping("/clearCache")
	 @ResponseBody
	 public Result<?> clearCache()
	 {
		 sysDictService.clearCache();
		 return Result.ok("清空缓存成功！");
	 }


}
